package com.wiscamp.ninechapters.problems.domain.services;

import com.wiscamp.ninechapters.problems.domain.models.exams.PaperPart;
import com.wiscamp.ninechapters.problems.domain.models.exams.PaperSection;
import com.wiscamp.ninechapters.problems.domain.repositories.PaperPartRepository;
import cube.common.cache.CacheManagerNames;
import cube.ddd.aspect.HandleServiceException;
import cube.ddd.domain.BaseDomainService;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

@Service
public class PaperPartService extends BaseDomainService {

    private final PaperPartRepository paperPartRepository;

    private final String CACHE_NAME = "paperParts";

    public PaperPartService(PaperPartRepository paperPartRepository) {
        this.paperPartRepository = paperPartRepository;
    }

    //region paper part

    /*@Cacheable(value = CACHE_NAME, key = "#partId", cacheManager = CacheManagerNames.SIXTY_MINUTES_CACHE)
    @HandleServiceException
    public PaperPart getPaperPart(@Min(0) long partId) {
        return paperPartRepository.getPaperPart(partId);
    }*/


    /*@HandleServiceException
    @Cacheable(value = "exams:paperParts", key = "#examId", cacheManager = CacheManagerNames.SIXTY_MINUTES_CACHE)
    public Collection<PaperPart> getPaperPartsByExam(@Min(0) long examId) {
        return paperPartRepository.getPaperPartsByExam(examId);
    }*/

    /**
     * Creates a paper part
     *
     * @param part
     * @return
     */

    @CachePut(value = CACHE_NAME, key = "#part.partId", cacheManager = CacheManagerNames.SIXTY_MINUTES_CACHE)
    @HandleServiceException
    public PaperPart createPaperPart(@NotNull PaperPart part, long operatorId) {
        //记录操作日志
        return paperPartRepository.savePaperPart(part);
    }

    /**
     * Updates a paper part
     *
     * @param part
     * @return
     */

    @CachePut(value = CACHE_NAME, key = "#part.partId", cacheManager = CacheManagerNames.SIXTY_MINUTES_CACHE)
    @HandleServiceException
    public PaperPart updatePaperPart(@NotNull PaperPart part, long operatorId) {
        //记录操作日志
        return paperPartRepository.savePaperPart(part);
    }

    /**
     * Deletes a paper part
     *
     * @param partId
     * @return
     */

    @CacheEvict(value = CACHE_NAME, key = "#partId")
    @HandleServiceException
    public boolean deletePaperPart(@Min(0) long partId, long operatorId) {
        //记录操作日志
        paperPartRepository.deletePaperPart(partId);
        return true;
    }
    //endregion

    //region paper section

    /*@HandleServiceException
    public PaperSection getPaperSection(@Min(0) long sectionId) {
        return paperPartRepository.getPaperSection(sectionId);
    }

    @HandleServiceException
    public Collection<PaperSection> getPaperSectionsByPart(@Min(0) long partId) {
        return paperPartRepository.getPaperSectionsByPart(partId);
    }*/


    @HandleServiceException
    public PaperSection createPaperSection(@NotNull PaperSection section, long operatorId) {
        //记录操作日志
        return paperPartRepository.savePaperSection(section);
    }

    @HandleServiceException
    public PaperSection updatePaperSection(@NotNull PaperSection section, long operatorId) {
        //记录操作日志
        return paperPartRepository.savePaperSection(section);
    }

    /**
     * Deletes a paper section
     *
     * @param sectionId
     * @return
     */

    @HandleServiceException
    public boolean deletePaperSection(@Min(0) long sectionId, long operatorId) {
        //记录操作日志
        paperPartRepository.deletePaperSection(sectionId);
        return true;
    }
    //endregion
}
